package com.hankarun.gevrek;

import org.apache.commons.net.nntp.Article;

import com.hankarun.gevrek.lib.ServerManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class PostActivity extends Activity {
	public static final String PREFS_NAME = "GevrekPrefs";
	private EditText fromview;
	private TextView subjectview;
	private EditText bodyview;
	private Button sendbutton;
	private Article post;
	private String username;
	private int value;
	
	protected ServerManager app;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post);
        
        fromview = (EditText) findViewById(R.id.fromTextPost);
        subjectview = (EditText) findViewById(R.id.subjectviewpost);
        bodyview = (EditText) findViewById(R.id.bodypost);
        sendbutton = (Button) findViewById(R.id.sendbuttonpost);
        
        app = (ServerManager)getApplication();
        
        Bundle extras = getIntent().getExtras();
        value = extras.getInt("reply");
        Log.d("value",value+"");
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        String realname = settings.getString("realname", "");
        username = settings.getString("username", "");

        if(realname.equals(""))
        	realname = username;
        
        fromview.setText(realname);
        
        if(value == 0){			
			if(app.carticle.article.getSubject().contains("Re:")||app.carticle.article.getSubject().contains("re:"))
				subjectview.setText(app.carticle.article.getSubject());
			else
				subjectview.setText("Re: "+app.carticle.article.getSubject());
        }
        
        sendbutton.setOnClickListener(new Button.OnClickListener() { 
        	public void onClick (View v){
        		if(subjectview.equals("")){
        			
        		}else{
        			SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        			SharedPreferences.Editor editor = settings.edit();
            		editor.putString("realname", fromview.getText().toString());
            		editor.commit();
	        		post = new Article();
	    			post.setFrom(username+"@ceng.metu.edu.tr"+" ("+fromview.getText().toString()+")");
	    			post.setSubject(subjectview.getText().toString());
	        		if(value == 0){
	        			String[] refs = app.carticle.article.getReferences();
	        			for (String r : refs)
	    					post.addReference(r);
	        			post.addReference(app.carticle.article.getArticleId());
	        		}
	        		ServerTask task = new ServerTask();
	    			task.execute();
        		}
    		}
    	});      
    }
  	 private class ServerTask extends AsyncTask<Void, Void, Void> {
	    	private ProgressDialog dialog;
	    	@Override
	    	protected void onPreExecute(){
	    		dialog = ProgressDialog.show(PostActivity.this, "", 
	    				getString(R.string.sending), true);
	    	}
	    	
	    	@Override
	    	protected void onPostExecute(Void s) {
	    		setResult(1);
	    		finish();
	    	    dialog.dismiss();
	    	}

	    	@Override
	    	protected Void doInBackground(Void... params) {
	    		app.postArticle(post, bodyview.getText().toString());
	        	return null;
	    	}
	   }
}

